package com.mustafaiev.tair.mweather;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

/**
 * Class Helper. Creation date: 4 Sep 2011 14:10:06
 * 
 * @author Tair Mustafaiev
 * 
 */
public class MWeatherCommonHelper {

	/**
	 * Method make encoding from Cyrillic letters to Latin. Creation date: 4 Sep
	 * 2011 14:14:18
	 * 
	 * @param word
	 * @return encoded string
	 */
	public static String translitWord(String word) {
		Map<String, String> literatesSet = new TreeMap<String, String>();

		literatesSet.put("а", "a");
		literatesSet.put("б", "b");
		literatesSet.put("в", "v");
		literatesSet.put("г", "g");
		literatesSet.put("д", "d");
		literatesSet.put("е", "e");
		literatesSet.put("ё", "yo");
		literatesSet.put("ж", "zh");
		literatesSet.put("з", "z");
		literatesSet.put("и", "i");
		literatesSet.put("й", "y");
		literatesSet.put("к", "k");
		literatesSet.put("л", "l");
		literatesSet.put("м", "m");
		literatesSet.put("н", "n");
		literatesSet.put("о", "o");
		literatesSet.put("п", "p");
		literatesSet.put("р", "r");
		literatesSet.put("с", "s");
		literatesSet.put("т", "t");
		literatesSet.put("у", "u");
		literatesSet.put("ф", "f");
		literatesSet.put("х", "kh");
		literatesSet.put("ц", "ts");
		literatesSet.put("ч", "ch");
		literatesSet.put("ш", "sh");
		literatesSet.put("щ", "shch");
		literatesSet.put("ъ", "\"");
		literatesSet.put("ы", "y");
		literatesSet.put("ь", "\'");
		literatesSet.put("э", "e");
		literatesSet.put("ю", "yu");
		literatesSet.put("я", "ya");
		literatesSet.put("-", "-");
		literatesSet.put(".", ".");
		literatesSet.put(" ", " ");

		word = word.toLowerCase();
		char[] charArray = word.toCharArray();
		ArrayList<String> translittedWordArray = new ArrayList<String>();
		StringBuilder builder = new StringBuilder();
		int i = 0;
		for (char c : charArray) {
			Character charSymbol = new Character(c);
			if (literatesSet.containsKey(charSymbol.toString())) {
				translittedWordArray
						.add(literatesSet.get(charSymbol.toString()));
				if (i == 0) {
					builder.append(translittedWordArray.get(i).toUpperCase());
				} else {
					builder.append(translittedWordArray.get(i));
				}
				i++;
			}

		}

		return builder.toString();

	}
}
